<!DOCTYPE html>
<html lang="en">
<head>
	<script>window.update = function(){};</script>
	<title>AudioMass - Frequency Analysis</title>
	<style>
	html,body{height:100%}
	body{padding:0;margin:0;background:#111;position:relative;z-index:2;overflow:hidden;}
	div{position:absolute;top:50%;left:0;right:0;z-index:1;margin-top:-18px;text-align:center;user-select:none;
		font-size:28px;color:#191919;pointer-events:none;font-family:Arial}
	#e, #d, #f{
		cursor:pointer;
		position: absolute;
	    top: 7px;
	    right: 7px;
	    display: block;
	    height: 20px;
	    z-index: 99999;
	    background: #eaeaea;
	    left:auto;
	    border-radius:4px;
		font-family: sans-serif;
	    font-size: 9px;
	    width: 82px;
	    line-height: 20px;
	    padding: 0 6px;
	    text-align: center;
	    opacity:0.9;
	    user-select:none;
	}

	.b #e, .b #d{
		top:12px;
	}

	.b #e, .b #f{
		display:block;
	}

	#e{
		right: 106px;
	    width: 10px;
	    line-height: 20px;
	    padding: 0px 6px;
	    text-align: center;
	    opacity: 0.9;
	    display:none;
	}

	#f{
		left: 0;
	    right: 0;
	    top: 0;
	    padding: 0;
	    width: 100%;
	    line-height: 0;
	    border-radius: 0;
	    height: 4px;
	    background: #333;
	    display:none;
	    user-select:none;
	    -moz-user-select:none;
	}

	.c #f, #f:hover{
		background:#3C3C3C;
	}

	</style>
	<meta charset="utf-8" />
</head>
<body>
	<canvas id="fr" width="600" height="188" style="width:100%;height:100%;display:block"></canvas>
	<div>FREQUENCY ANALYSER</div>

	<a id="d" onclick="dock()">DOCK INTERFACE</a>
	<a id="e" onclick="remove()">X</a>
	<a id="f" onmousedown="drag(event)"></a>

	<script>
		var d = document;
		var w = window;
		var iframe = location.href.indexOf('?iframe') === -1 ? 0 : 1;
		var canvas = d.getElementById('fr');
		var ctx = canvas.getContext('2d', {alpha:true, antialias:false});

		w.remove = function (){};
		if (iframe) {
			d.body.className = 'b';
			w.remove = function () {
				w.parent.PKAudioEditor.ui.Dock ('RequestShowFreqAn', 'eq', [1, 1]);
			};
		};

		w.drag = function ( e ) {
			e.preventDefault ();
			e.stopPropagation ();

			w.parent.PKAudioEditor.ui.Dock ('RequestDragI', 'eq');
		};

		w.dock = function () {
			if (!iframe)
			{
				if (!w.opener || !w.opener.PKAudioEditor) {
					return ;
				}

				w.opener.PKAudioEditor.ui.Dock ('RequestShowFreqAn', 'eq', [1, 1]);
				w.close && w.close ();
			}
			else
			{
				var frm = w.parent.document.getElementById ('pk_fr' + 'eq');
				var t = 1;

				if (frm && frm.getBoundingClientRect) {
					var rect = frm.getBoundingClientRect();

					t = [(w.parent.screenLeft + rect.left + 100)||0, (w.parent.screenTop + rect.top + 25)||0];
				}

				w.parent.PKAudioEditor.ui.Dock ('RequestShowFreqAn', 'eq', [t, 0]);
			}
		};

		setTimeout(function () {
			if (!iframe)
			{
				if (!w.opener || !w.opener.PKAudioEditor) return ;
			}

			var WIDTH = w.innerWidth, HEIGHT = w.innerHeight;

			if (canvas.width != WIDTH)
			{
				canvas.width = WIDTH;
				canvas.height = HEIGHT;
			}

			var bufferLength = 240;
			var value_changed = false;
			ctx.clearRect (0, 0, WIDTH, HEIGHT);

			function draw( similarity ) {

				var WIDTH = window.innerWidth, HEIGHT = window.innerHeight;

				if (canvas.width != WIDTH || canvas.height != HEIGHT)
				{
					canvas.width = WIDTH;
					canvas.height = HEIGHT;
				}

				  ctx.fillStyle = 'rgb(0, 0, 0)';
				  ctx.fillRect(0, 0, WIDTH, HEIGHT);

				  var barWidth = (WIDTH / bufferLength);
				  var barHeight;
				  var x = 0;

				  for(var i = 0; i < bufferLength; i += 1) {
					barHeight = similarity[i * 2]; // ? similarity[i] : 0;

					// bar height normalize with 255
					var newheight = ((barHeight / 256) * HEIGHT) >> 0;

					ctx.fillStyle = 'rgb(' + (barHeight + 100) + ',50,50)';
					ctx.fillRect (x,HEIGHT - newheight, barWidth, newheight);
					x += barWidth;// + 1;
				  }

				  value_changed = false;
			};
			w.draw = draw;

			w.onunload = function () {
				w.destroy && w.destroy ( iframe );
				w.destroy = null;
			};

			w.update = function (freq_arr) {
				if (!freq_arr)
					ctx.clearRect (0, 0, WIDTH, HEIGHT);
				else {
					if (value_changed) return ;
					value_changed = true;

					window.requestAnimationFrame(function () {
						draw (freq_arr);
					});
				}
			};

			var last_press = 0;
			document.addEventListener ('keypress', function ( e ) {
				if (e.keyCode !== 32) return ;

				e.preventDefault ();
				e.stopPropagation ();

				if (e.timeStamp - last_press < 100) {
					return ;
				}

				last_press = e.timeStamp;

				if (!iframe) {
					w.opener && w.opener.PKAudioEditor.ui.Dock ('RequestKeyDown', 32);
				}
				else {
					w.parent && w.parent.PKAudioEditor.ui.Dock ('RequestKeyDown', 32);
				}
			});

		}, 60);
	</script>

</body>
</html>